<template>
  <div id="user">
    <!-- 按钮和查询 -->
    <div class="addAndCheck">
      <el-button @click="addClick" type="primary">+ 新增</el-button>
      <el-form :inline="true" :model="userCheckForm">
        <el-form-item>
          <el-input placeholder="请输入" v-model="userCheckForm.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 提示框 - 新增和编辑 -->
    <el-dialog
      title="新增用户"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="50%"
    >
      <!-- 用户表单信息 -->
      <el-form ref="form" :rules="rules" :inline="true" :model="form">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            v-model="form.birth"
            type="date"
            placeholder="选择日期时间"
            value-format="yyyy-MM-DD"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>
      <!-- 确认和取消按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelClick">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 表格数据和分页功能 -->
    <div class="table-pagination">
      <!-- table表格的用户数据 -->
      <el-table height="90%" :data="tableData" stripe style="width: 100%">
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="sex" label="性别">
          <!-- element-ui中的自定义 - 使用的是作用域插槽,可以让父组件用子组件的数据 -->
          <template slot-scope="scope">
            <span>{{ scope.row.sex === 1 ? "男" : "女" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄"> </el-table-column>
        <el-table-column prop="birth" label="出生日期"> </el-table-column>
        <el-table-column prop="addr" label="地址"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 - 每页显示个数默认为10条 -->
      <div class="pagination">
        <el-pagination
          @current-change="handlePageClick"
          layout="prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
// 获取接口数据
import { getUser, addUser, editUser, delUser } from "../api/index.js";
export default {
  name: "User",
  mounted() {
    this.getUserData();
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
        age: [
          {
            required: true,
            message: "请输入年龄",
            trigger: "blur",
          },
        ],
        sex: [
          {
            required: true,
            message: "请选择性别",
            trigger: "change",
          },
        ],
        birth: [
          {
            required: true,
            message: "请选择出生日期",
            trigger: "change",
          },
        ],
        addr: [
          {
            required: true,
            message: "请输入地址",
            trigger: "blur",
          },
        ],
      },
      tableData: [],
      // 因为编辑弹框和新增弹框是可复用的,所以可以定义一个modelType来判断点击的是哪一个
      modelType: 0, // 0表示新增按钮的弹框,1表示编辑按钮的弹框
      total: 0, // 分页总页数
      pageData: { page: 1, limit: 10 },
      // 查询数据
      userCheckForm: {
        name: '',
      },
    };
  },
  methods: {
    // 获取用户数据列表
    getUserData() {
      // getUser方法返回一个promise对象
      // 传入参数 - params: {{ page = 1, limit = 10 }} - 第几页,一页显示几条数据
      getUser({ params: {...this.userCheckForm,...this.pageData} }).then(({ data }) => {
        console.log(data);
        this.tableData = data.list;
        // 获取一共有多少条数据
        this.total = data.count || 0;
      });
    },
    // 分页按钮
    handlePageClick(val) {
      // val为点击的页数
      console.log(val);
      // 修改页数,使得每一页都显示10条数据
      this.pageData.page = val;
      // 更新数据
      this.getUserData();
    },
    // 查询功能
    onSubmit() {
      this.getUserData();
    },
    addClick() {
      this.modelType = 0;
      this.dialogVisible = true;
    },
    submit() {
      // 对整个表单进行校验.参数为一个回调函数,valid为布尔值
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 如果为0 - 新增弹框,则连接新增用户数据的接口
          // 若为1 - 编辑弹框,则连接更新用户数据的接口
          if (this.modelType === 0) {
            // 先提交数据,再获取新的用户数据列表
            addUser(this.form).then(() => {
              this.getUserData();
            });
            // 重置表单数据
            this.$refs.form.resetFields();
          } else {
            // 不需要重置表单数据 - 传入的this.form为当前行的数据
            editUser(this.form).then(() => {
              this.getUserData();
            });
          }
          this.dialogVisible = false;
        }
      });
    },
    // 弹窗关闭的时候触发
    handleClose() {
      console.log(this.$refs.form);
      // 重置表单数据
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    // 取消按钮
    cancelClick() {
      this.handleClose();
    },
    // 更改用户信息
    handleEdit(row) {
      this.modelType = 1;
      this.dialogVisible = true;
      // 对row进行深拷贝- 因为要处理row的数据且不能影响row原本的数据
      this.form = JSON.parse(JSON.stringify(row));
    },
    // 删除用户信息
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 更新表格数据
            this.getUserData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="less" scoped>
#user {
  height: 90%;
  .addAndCheck {
    display: flex;
    justify-content: space-between;
    :first-child {
      height: 40px;
    }
  }
  .table-pagination {
    position: relative;
    // 32px - 指的是,分页部分的高度
    height: calc(100% - 32px);
    .pagination {
      position: absolute;
      bottom: 0;
      right: 20px;
    }
  }
}
</style>
